import "reflect-metadata";

// 装饰器类型
export enum DecoratorKey {
    Controller = "controller",
    Method = "method",
}
// 请求类型
export enum MethodTyp {
    Get = "get",
    POST = "post"
}

// 请求装饰器元数据类型
export interface MethodMetadata {
    method: MethodTyp,
    route: string,
    fn: Function
}


/**
 * controller 只用于收集路由前缀
 * @param name 一般为文件名全小写 
 * @param prefix 路径前缀，选填，默认为`/${name}`
 * @returns 
 */
export const controller = (name: string, prefix?: string,) => (target: any) => {
    if (!prefix) {
        prefix = "/" + name;
    }
    Reflect.defineMetadata(DecoratorKey.Controller, { name, prefix }, target);
}



// 创建工厂
const method = (method: any) => {
    return (route: string) => {
        return (target: any, key: string, descriptor: PropertyDescriptor) => { 
            Reflect.defineMetadata(DecoratorKey.Method, {
                method, route, fn: descriptor.value
            }, target, key)
        }
    }
}

/**
 * 创建一个get请求
 * @param route 理由路径
 */
export const get = method(MethodTyp.Get);

/**
 * 创建一个post请求
 * @param route 理由路径
 */
export const post = method(MethodTyp.POST);

